#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
int n, m, par[maxn], sz[maxn];
typedef array<int,3> edge;
vector<edge> edges;
int find(int u) {
  return u==par[u]?u:par[u]=find(par[u]);
}
/*void merge(int u, int v) {
  u=find(u);v=find(v);
  if(u==v)return;
  if(sz[u]>sz[v])swap(u,v);
  par[u]=v;
  sz[v]+=sz[u];
  // num_grp--;
}*/
void kruskal() {
  sort(edges.begin(), edges.end());
  for(int i=1; i<=n; ++i) par[i]=i,sz[i]=1;
  ll cnt=0,cost=0;
  for(auto [w,u,v]:edges) {
    u=find(u), v=find(v);
    if(u!=v) {
      par[u]=v;
      cost+=w;
      cnt++;
    }
  }
  if(cnt==n-1)cout<<cost<<'\n';
  else cout<<"IMPOSSIBLE\n";
}
void solve() {
  kruskal();
}
int main() {
  cin>>n>>m;
  for(int i=0,u,v,w; i<m; ++i) {
    cin>>u>>v>>w;edges.push_back({w, u, v});
  }
  solve();
}
